package com.yao.shop.controller;

import com.yao.shop.common.Page;
import com.yao.shop.common.Result;
import com.yao.shop.pojo.Dto.OrderDto;
import com.yao.shop.pojo.ShopOrder;
import com.yao.shop.pojo.ShopOrderHistory;
import com.yao.shop.pojo.vo.ShopOrderVo;
import com.yao.shop.service.ShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class ShopOrderController {
    @Autowired
    private ShopOrderService shopOrderService;
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public Result<?> createOrder(@RequestBody OrderDto orderDto) {
        shopOrderService.createOrder(orderDto);
        return new Result<>("订单提交成功");
    }



    /**
     * 前台分页查询
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/getMyOrder", method = RequestMethod.POST)
    public Result<Page<ShopOrderVo>> getMyOrder(@RequestBody Page<ShopOrderVo> page) {
        page = shopOrderService.getMyOrder(page);
        return new Result<>(page);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<ShopOrderVo> get(@PathVariable Long id) {
        ShopOrderVo vo = shopOrderService.get(id);
        return new Result<>(vo);
    }

    /**
     * 根据id收货
     * @param id
     * @return
     */
    @RequestMapping(value = "/receiveById/{id}", method = RequestMethod.PUT)
    public Result<?> receiveById(@PathVariable Long id) {
        shopOrderService.receiveById(id);
        return new Result<>("收货成功");
    }

    /**
     * 根据id收货
     * @param id
     * @return
     */
    @RequestMapping(value = "/payById/{id}", method = RequestMethod.PUT)
    public Result<?> payById(@PathVariable Long id) {
        shopOrderService.payById(id);
        return new Result<>("收货成功");
    }
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<ShopOrder>> getByPage(@RequestBody Page<ShopOrder> page) {
        page = shopOrderService.getByPage(page);
        return new Result<>(page);
    }

    /**
     * 改变订单状态
     *
     * @param order
     * @return
     */
    @RequestMapping(value = "/changeOrderStatus", method = RequestMethod.PUT)
    public Result<?> changeOrderStatus(@RequestBody ShopOrder order) {
        shopOrderService.changeOrderStatus(order);
        return new Result<>("操作成功");
    }

    /**
     * 查询订单历史
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getHistory/{id}", method = RequestMethod.GET)
    public Result<List<ShopOrderHistory>> getHistory(@PathVariable Long id) {
        List<ShopOrderHistory> list = shopOrderService.getHistory(id);
        return new Result<>(list);
    }
}
